const express = require('express');
const { body, validationResult } = require('express-validator');
const { authMiddleware, adminMiddleware } = require('../middlewares/auth.middleware');
// const Setting = require('../models/setting.model'); // 不再直接使用 Model
const settingsController = require('../controllers/settings.controller'); // 引入控制器
const router = express.Router();

// --- Validation Chains (可以移到控制器或保持在这里) ---
const validateBasicSettings = [
    body('siteName').optional().trim().isLength({ min: 1, max: 100 }),
    body('siteDescription').optional().trim().isLength({ max: 500 }),
    // body('siteKeywords').optional().trim().isLength({ max: 200 }), // 假设模型中没有 keywords
    body('logo').optional().trim().isURL(), // 修改为 logo
    body('icp').optional().trim(), // 修改为 icp
    body('copyright').optional().trim(), // 新增 copyright
    // body('allowRegistration').optional().isBoolean(), // 假设模型中没有
    // body('requireEmailVerification').optional().isBoolean() // 假设模型中没有
];

const validateEmailSettings = [
    body('smtpHost').optional().trim().isLength({ min: 1 }),
    body('smtpPort').optional().isInt({ min: 1, max: 65535 }),
    body('secure').optional().isBoolean(), // 修改为 secure
    body('fromEmail').optional().trim().isEmail(), // 新增 fromEmail
    body('fromName').optional().trim(), // 新增 fromName
    body('username').optional().trim(), // 修改为 username
    body('password').optional().trim().isLength({ min: 1 }) // 修改为 password
];

const validateStorageSettings = [
    body('type').optional().isIn(['local', 'oss', 'cos']), // 修改为 type, 添加 cos
   
    body('accessKeyId').optional().trim(),
    body('accessKeySecret').optional().trim(), 
    body('bucket').optional().trim(), 
    body('region').optional().trim(), 
    body('domain').optional().trim().isURL().optional({ nullable: true, checkFalsy: true }), // 新增 domain
    body('secretId').optional().trim(), // 新增 secretId (for COS)
    body('secretKey').optional().trim() // 新增 secretKey (for COS)
];

// --- Routes using Controller ---

// 获取所有设置 (由控制器处理权限和返回内容)
router.get('/', authMiddleware, adminMiddleware, settingsController.getSettings);

// 更新设置 (由控制器处理更新逻辑)
router.put('/',
  authMiddleware,
  adminMiddleware,
  // 包含所有可能的验证规则，控制器内部根据 req.body 的字段进行更新
  validateBasicSettings,
  validateEmailSettings,
  validateStorageSettings,
  settingsController.updateSettings
);

// 新增：上传 Logo 路由
router.post('/upload-logo', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.uploadLogo // 指向新的控制器方法
);

// 新增：测试邮件发送路由
router.post('/test-email', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.testEmail
);

// 新增：测试存储连接路由
router.post('/test-storage', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.testStorage
);

// 新增：系统维护相关路由
router.post('/clear-cache', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.clearCache
);
router.post('/backup', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.backup
);
router.get('/backups', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.getBackups
);
router.post('/restore/:backupId', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.restoreBackup
);
router.delete('/backup/:backupId', 
    authMiddleware, 
    adminMiddleware, 
    settingsController.deleteBackup
);


module.exports = router;